package com.example.weatherreport;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherreport.bean.WeatherForecastBean;
import com.example.weatherreport.utils.TimeUtils;
import com.example.weatherreport.utils.WeekdayConverter;

import java.text.ParseException;
import java.util.List;

public class WeatherFragAdapter extends RecyclerView.Adapter<WeatherFragAdapter.ViewHolder> {

    private List<WeatherForecastBean> itemList;
    private static Context context;

    /**
     * 定义构造方法，传入数据
     **/
    public WeatherFragAdapter(List<WeatherForecastBean> itemList) {
        this.itemList = itemList;
    }

    /**
     * 用于创建一个选项模板
     * 参数1：父布局也就是RecyclerView控件。 参数2：选项类型
     **/
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        //动态加载选项布局文件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_center_vertical, parent, false);
        ViewHolder holder = new ViewHolder(view);   //创建选项模板
        return holder;                  //把选项模板返回给适配器类

    }

    /**
     * 用于给选项添加数据
     * 参数1：选项模板ViewHolder。 参数2：当前选项在列表里的下标
     **/
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 根据选项下标，安排对应数据。
        // 获取对应的位置的天气情况
        String time = itemList.get(position).getTime();
        holder.date.setText(time.substring(5));            //把数据放进选项控件里
        int resourceId = getWeatherIcon(itemList.get(position).getDay_weather());
        //System.out.println("当前为id："+resourceId);
        String currentDay = itemList.get(position).getCurrentDay();
        if (currentDay.contains("昨") || currentDay.contains("今")
                || currentDay.contains("明")) {
            holder.date2.setText(itemList.get(position).getCurrentDay());  //把数据放进选项控件里
        } else {
            String weekday;
            try {
                Integer weekdayInt = TimeUtils.getWeekdayFromInput(time);
                weekday = WeekdayConverter.convertToChinese(weekdayInt);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            holder.date2.setText(weekday);  //把数据放进选项控件里
        }

        holder.iv.setImageResource(resourceId); //把数据放进选项控件里
        holder.max_tv.setText(itemList.get(position).getMax_degree()); //把数据放进选项控件里
        holder.min_tv.setText(itemList.get(position).getMin_degree()); //把数据放进选项控件里
    }

    /**
     * 用于设置列表选项的个数或长度
     **/
    @Override
    public int getItemCount() {
        return itemList.size();//设置列表选项的个数为数据集合的长度
    }

    /**
     * 用于存放选项模板布局及其控件
     **/
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView date;
        TextView date2;
        ImageView iv;
        TextView max_tv;
        TextView min_tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.item_center_tv_date_vertical);   //获得选项布局里TextView控件
            date2 = itemView.findViewById(R.id.item_center_tv_date2_vertical);   //获得选项布局里TextView控件
            iv = itemView.findViewById(R.id.frag_iv_weather_icon_forecast_vertical);   //获得选项布局里TextView控件
            max_tv = itemView.findViewById(R.id.item_center_tv_temp_vertical);   //获得选项布局里TextView控件
            min_tv = itemView.findViewById(R.id.item_center_tv_temp2_vertical);   //获得选项布局里TextView控件
        }

    }


    /**
     * 雨、雪、沙尘暴，雷阵雨、雨夹雪、多云
     *
     * @param shortReport
     * @return
     */
    private int getWeatherIcon(String shortReport) {

        if (shortReport == null || shortReport.length() == 0) {
            return 0;
        }
        String temp = "sunny";
        if (shortReport.contains("雨")) {
            temp = "rainy";
        }
        if (shortReport.contains("雪")) {
            temp = "snowy";
        }
        if (shortReport.contains("沙")) {
            temp = "sandstorm";
        }
        if (shortReport.contains("雷阵雨")) {
            temp = "thunderstorm";
        }
        if (shortReport.contains("雨夹雪")) {
            temp = "sleet";
        }
        if (shortReport.contains("云") || shortReport.contains("阴")) {
            temp = "cloudy";
        }
        if (shortReport.contains("雾")) {
            temp = "foggy";
        }
        // 获取图片资源ID
        int tempId;
        tempId = context.getResources().getIdentifier(temp, "mipmap", context.getPackageName());
        return tempId;
    }
}

